package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.CartsUpdateForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @description:
 * @author:
 * @date:
 */

@RestController
@RequestMapping("/shopping")
public class CartController {

    @Reference(check = false)
    ICartService iCartService;


    @GetMapping("cart")
    public ResponseData getCartList1(HttpServletRequest servletRequest) {
        return getCartList(servletRequest);
    }


    @GetMapping("carts")
    public ResponseData getCartList(HttpServletRequest servletRequest) {
        Long userIdFromServlet = Utils.getUserIdFromServlet(servletRequest);

        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(userIdFromServlet);

        CartListByIdResponse response = iCartService.getCartListById(cartListByIdRequest);


        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getCartProductDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    @PostMapping("carts")
    public ResponseData addToCartList(@RequestBody AddCartRequest addCartRequest) {


        AddCartResponse response = iCartService.addToCart(addCartRequest);


        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 购物车更新
     * zhiMing 2021年12月13日 09点45分
     *
     * @param updateForm
     * @param servletRequest
     * @return
     */
    @PutMapping("/carts")
    public ResponseData cartsUpdate(@RequestBody CartsUpdateForm updateForm, HttpServletRequest servletRequest) {
        UpdateCartNumRequest request = new UpdateCartNumRequest();
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        request.setUserId(uid);
        request.setChecked(updateForm.getChecked());
        request.setItemId(updateForm.getProductId());
        request.setNum(updateForm.getProductNum());
        UpdateCartNumResponse response = iCartService.updateCartNum(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/carts/{uid}/{pid}")
    public ResponseData cartsDelete(@PathVariable("uid") Long uid,@PathVariable("pid") Long pid) {
        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setItemId(pid);
        request.setUserId(uid);
        DeleteCartItemResponse response = iCartService.deleteCartItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/items/{uid}")
    public ResponseData cartsDeleteByCheck(@PathVariable("uid") Long userId) {
        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(userId);
        DeleteCheckedItemResposne response = iCartService.deleteCheckedItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @PutMapping("/items")
    public ResponseData cartsAllChecked(@RequestBody Map map) {
        CheckAllItemRequest request = new CheckAllItemRequest();
        request.setUserId(Long.parseLong(map.get("userId").toString()));
        request.setChecked(String.valueOf(map.get("checked")));
        CheckAllItemResponse response = iCartService.checkAllCartItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}

class Utils {

    public static Long getUserIdFromServlet(HttpServletRequest servletRequest) {
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        return uid;
    }

}
